package com.example.demo.service;

import com.example.demo.model.Commodity;
import com.example.demo.repository.ComRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 宇宙第一帅
 * @Date: 2019/3/27 14:57
 * @Description:
 */
@Service
public class ComServiceImpl implements ComService{
    @Autowired
    private ComRepository ComRepository;

    /**
     *
     * @param id 商品编号
     * @param type 商品类型
     * @param name 商品名称
     * @param price 商品价格
     * @param introduce 商品介绍
     * @param picture 商品图解
     * @return
     */
    @Override
    public Integer insert(String id, String type, String name, String price, String introduce, String picture) {
        Commodity com = new Commodity();
        com.setId(id);
        com.setType(type);
        com.setName(name);
        com.setPrice(price);
        com.setIntroduce(introduce);
        com.setPicture(picture);
        try{
        Commodity save = ComRepository.save(com);
        if(save != null){
            return 1;
        }else {
            return 2;
        }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer delete(String id) {
        try{
            Commodity com =  ComRepository.findById(id).get();
            if(com != null){
                ComRepository.deleteById(id);
                return 1;
            }else{
                return 2;
            }
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public Integer update(Commodity commodity) {
        try {
            Commodity com = ComRepository.findById(commodity.getId()).get();
            if(com != null){
                ComRepository.save(commodity);
                return 1;
            }else{
                return 2;
            }

        }catch (Exception e){
            e.printStackTrace();
        return 0;
        }
    }

    @Override
    public List<Commodity> select() {

        return ComRepository.findAll();
    }
}
